<!--
 * @Author: wangzhichiao<https://github.com/wzc570738205>
 * @Date: 2021-05-10 13:35:26
 * @LastEditors: wangzhichiao<https://github.com/wzc570738205>
 * @LastEditTime: 2021-05-10 15:45:44
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      body {
        background: black;
        color: #cccccc;
      }
      #c3 {
        background-image: url(./img.jpg);
        background-repeat: no-repeat;
      }
      div {
        float: left;
        border: 1px solid #444444;
        padding: 10px;
        margin: 10px;
        background: #3b3b3b;
      }
    </style>
    <script>
      var processor = {
        doLoad: function doLoad() {
          this.video = document.getElementById("video");
          this.c1 = document.getElementById("c1");
          this.ctx1 = this.c1.getContext("2d");
          this.c2 = document.getElementById("c2");
          this.ctx2 = this.c2.getContext("2d");
          this.c3 = document.getElementById("c3");
          this.ctx3 = this.c3.getContext("2d");
          let self = this;
          this.video.addEventListener(
            "play",
            function () {
              self.width = self.video.videoWidth / 5;
              self.height = self.video.videoHeight / 3;
              self.timerCallback();
            },
            false
          );
        },
        timerCallback: function timerCallback() {
          if (this.video.paused || this.video.ended) {
            return;
          }
          this.computeFrame();
          let self = this;
          setTimeout(function () {
            self.timerCallback();
          }, 0);
        },
        computeFrame: function computeFrame() {
          this.ctx1.drawImage(this.video, 0, 0, this.width, this.height);
          let frame = this.ctx1.getImageData(0, 0, this.width, this.height);
          let l = frame.data.length / 4;

          for (let i = 0; i < l; i++) {
            let r = frame.data[i * 4 + 0];
            let g = frame.data[i * 4 + 1];
            let b = frame.data[i * 4 + 2];
            //rgb(8 204 4)
            if (r > 4 && g > 100 && b < 100) {
              frame.data[i * 4 + 3] = 0;
            }
          }
          this.ctx2.putImageData(frame, 0, 0);
          this.ctx3.putImageData(frame, 0, 0);
          return;
        },
      };
    </script>
  </head>
  <body onload="processor.doLoad()">
    <div>
      <video id="video" src="./video.mp4" width="350" controls="true"></video>
    </div>
    <div>
      <!-- 视频截图 -->
      <canvas id="c1" width="260" height="190"></canvas>
      <!-- 处理绿色像素为透明 -->
      <canvas id="c2" width="260" height="190"></canvas>
      <!-- 贴图至背景图上方 -->
      <canvas id="c3" width="260" height="190"></canvas>
    </div>
  </body>
</html>
